package com.utils.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexPattern {
	public static void main(String[] args) {
		String str = "service@xsoftlab.net";
		String regEx = "[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";
		System.out.println(pattern(str, regEx));
	}
	
	/**
	 * 校验字符串是否符合规则
	 * @param str 要验证的字符串
	 * @param regex 验证规则
	 * @return
	 */
	public static boolean pattern(String str,String regex) {
	    // 编译正则表达式
	    Pattern pattern = Pattern.compile(regex);
	    // 忽略大小写的写法
	    // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
	    Matcher matcher = pattern.matcher(str);
	    // 字符串是否与正则表达式相匹配
	    boolean match = matcher.matches();
		return match;
	}
}
